<?php


namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\ImagesModel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

/**
 * Class ImagesDao
 * @package App\Dao
 * author:fhc
 * desc:轮播图Dao
 * created_at: 2020/11/20 - 4:27 下午
 */
class ImagesDao extends BaseDao
{
    /**
     * @param array $params
     * author:fhc
     * desc:获取轮播图Builder
     * created_at: 2020/11/20 - 4:27 下午
     */
    public static function getImagesBuilder($params = []): Builder
    {

        $query = ImagesModel::query();

        $query->when(checkIsHasField($params,'id'),function ($query)use ($params){

            $query->where('id',$params['id']);

        });


        $query->when(checkIsHasField($params,'show_type'),function ($query)use ($params){

            $query->where('show_type',$params['show_type']);

        });


        $query->when(checkIsHasField($params,'type'),function ($query)use ($params){

            $query->where('type',$params['type']);

        });


        $query->when(checkIsHasField($params,'show'),function ($query)use ($params){

            $query->where('show',$params['show']);

        });


        return $query->orderByDesc('images_sort')->orderByDesc('created_at');
    }

    /**
     * author:fhc
     * desc:获取分页
     * created_at: 2020/11/20 - 7:22 下午
     */
    public static function getImagesPager(): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::getImagesBuilder()->paginate();
    }

    /**
     * @param array $all
     * @throws SysExceptions
     * author:fhc
     * desc:添加
     * created_at: 2020/11/20 - 7:50 下午
     */
    public static function add(array $all)
    {
        if(self::getImagesBuilder()->create($all) === false){
            self::error('添加失败');
        }
    }

    /**
     * @param array $all
     * author:fhc
     * desc:修改
     * created_at: 2020/11/20 - 8:07 下午
     * @throws SysExceptions
     */
    public static function update(array $all)
    {
        if(self::getImagesBuilder(['id'=>self::deleteParamId($all)])->update(self::filterFields($all,new ImagesModel())) === false){
            self::error('修改失败');
        }
    }

    /**
     * @param array $all
     * author:fhc
     * desc:图片
     * created_at: 2020/11/30  22:13
     * @return Builder[]|Collection
     */
    public static function getImagesList(array $all)
    {
        $list =  self::getImagesBuilder($all)->get()->toArray();
        foreach ($list as &$value){
            $item = [
                'project_info'=>[],
                'vip_info'=>[]
            ];
            if($value['click_type'] == 1){
                $item['vip_info'] = VipSettingsDao::get_vip_info([
                    'vip_id'=>$value['click_data']
                ]);
            }else if($value['click_data'] == 2){
                $item['project_info'] = ProjectDao::getOne([
                    'project_id'=>$value['click_data']
                ]);
            }
            $value = array_merge($value,$item);
        }
        return $list;
    }

}
